package com.bqtt.tumor.demo.presentation.controller;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.bqtt.tumor.demo.business.ImageService;
import com.bqtt.tumor.demo.business.ImageService;
import com.bqtt.tumor.demo.common.utility.InlineTools;
import com.bqtt.tumor.demo.entity.Image;
import com.bqtt.tumor.demo.entity.Image;
import com.bqtt.tumor.demo.entity.SystemUser;

public class image {

	public static String queryImage( SystemUser user,  ModelMap model, Image condition,Integer case_id,String information1,String information2,String information3)
	{
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("user"))
				model.addAttribute("user", new SystemUser());
			return "login";
		}
		
		if(condition==null)
		{
			condition = new Image();
		}
		
		condition.setCase_id(case_id);
		condition.fillStringField();
		
		if(!model.containsAttribute("new_image"))
		{
			 Image temp = new Image();
			 temp.setCase_id(case_id);
		     model.addAttribute("new_image", temp);		
		}
		
		model.addAttribute("mri_image_list",ImageService.searchByCaseAndType(case_id,1));
		model.addAttribute("ct_image_list",ImageService.searchByCaseAndType(case_id,2));
		model.addAttribute("ct3d_image_list",ImageService.searchByCaseAndType(case_id,3));
		
		model.addAttribute("historySearchCondition", condition);
		
		model.addAttribute("condition", condition); //保留上次查找的字段
		
	    model.addAttribute("isAddNewShow","false");
	 
	    model.addAttribute("information1", information1 );
	    model.addAttribute("information2", information2 );
	    model.addAttribute("information3", information3 );
	    
		return "image";
	}
	
	
	public static ModelAndView uploadImage(SystemUser user, ModelMap model,Image imageEntity, MultipartFile imageFile,BindingResult bindingResult)
	{
		//session 变量 ——不用自动添加到Model
				//condition currentUser
			
		        
				
				String typeString = imageEntity.getType()+"";
				int case_id = imageEntity.getCase_id();
				
				if(bindingResult.hasErrors())
				{
					;
				}
				
				if(user.getSessionState() ==0)
				{// 用户未登入
				    return new ModelAndView("redirect:/");
				}
				

				if(imageFile==null)
				{
					// 添加错误后跳转
					model.addAttribute("information"+typeString, "<font style='color:#FF3333'>图像不为空！</font>");
					
					return new ModelAndView("redirect:queryImage?case_id="+case_id);
				}
				
				
				//验证图片
				String validateResult = InlineTools.validdateImage(imageFile);
				if(validateResult.equals("success"))
				{
					long  temp = System.currentTimeMillis(); 
					//成功则保存图片
					String saveResult = InlineTools.saveImage(temp+""+imageEntity.getCase_id(),imageFile);
					if(saveResult.equals("-1"))
					{
						model.addAttribute("information"+typeString, "<font style='color:#FF3333'>图像存储失败，请确保有权限，并且服务器存储空间足够！</font>");
						return new ModelAndView("redirect:queryImage?case_id="+case_id);
					}
					else
					{
						imageEntity.setImage_url(saveResult);
					}
				}
				else
				{  //错误提示
					model.addAttribute("information"+typeString, "<font style='color:#FF3333'>"+validateResult+"</font>");
					return new ModelAndView("redirect:queryImage?case_id="+case_id);
				}
				
				
				boolean result = ImageService.add(imageEntity);
				
				
				if(result)
				{		
					model.addAttribute("information"+typeString, "<font style='color:#5599FF'>操作成功！若页面无展示，请稍后刷新页面查看！</font>");
				}
				else
				{
					model.addAttribute("information"+typeString, "<font style='color:#FF3333'>操作失败！</font>");
				}
				
				return new ModelAndView("redirect:queryImage?case_id="+case_id);
	}
	
	
	public static ModelAndView deleteImage(SystemUser user, ModelMap model, Integer case_id, Integer image_def_id,Integer type, BindingResult bindingResult)
	{
		//session 变量 ——不用自动添加到Model
				//condition currentUser
			

				
				if(bindingResult.hasErrors())
				{
					;
				}
				
				if(user.getSessionState() ==0)
				{// 用户未登入
				    return new ModelAndView("redirect:/");
				}
				
				if(case_id ==null || image_def_id==null || type == null)
				{
					model.addAttribute("information1", "<font style='color:#FF3333'>删除失败：参数错误，请不要伪造参数！</font>");
				}
				
				Image transientInstance = ImageService.getById(image_def_id);

				boolean result = ImageService.delete(transientInstance);
				
				if(result)
					model.addAttribute("information1", "<font style='color:#5599FF'>删除成功</font>");
				else
					model.addAttribute("information1", "<font style='color:#FF3333'>删除失败，未知错误。！</font>");
				return new ModelAndView("redirect:queryImage?case_id="+case_id);
	}
	
	
}
